// pages/curriculum/curriculum.ts
import { Course, Curriculum } from "./interface";
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import { goToSignInCourse } from "../signIn/util";
import User from "../../utils/user";
import { curriculumInit } from "../../utils/request/api";
import msg from "../../utils/msg";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array_weeks: ['第1周', '第2周', '第3周', '第4周', '第5周', '第6周', '第7周', '第8周','第9周', '第10周','第11周', '第12周', '第13周', '第14周', '第15周', '第16周', '第17周', '第18周',],
    startDate: "",
    nowWeeks: 1 as number,
    sign_valid:true,
    curriculum_msg: [] as Array<Course>,
    precise: ['2/26', '2/27', '2/28', '2/29', '2/30', '3/1', '3/2'] as Array<string>,//储存周几下面显示的精确日期
    curriculum: {
      "一": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "二": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "三": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "四": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "五": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "六": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "七": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
    } as Curriculum,//课程表前端内容
    colors: [
      "#AEEC34",
      "#FFC44F",
      "#85B0FD",
      "#FEA17C",
      "#FF9392",
      "#D48DF9",
      "#7FCFF8",
      "#85B8CF",
      "#90C652",
      "#D8AA5A",
      "#FC9F9D",
      "#0A9A84",
      "#61BC69",
      "#12AEF3",
      "#E29AAD"] as Array<string>,
  },
  /**
  * @function 跳转到对应的课程签到
  * @param {*e} 
  */
  async _goToSignIn(e:any){
    console.log('e=',e);
    let id = e.currentTarget.dataset.id;
    let course:Course=this._getCouresFromId(id,this.data.curriculum_msg);
    if(!course.name) return;
    //弹出课程详情：用户选择是否签到->签到详情
    let title:string = '是否进入签到界面';
    let message:string = '课程:'+course.name+' 教师:'+course.teacher+' 地点:'+course.location;
    Dialog.confirm({
      title: title,
      message:  message,
    }).then(async (res) => {
      goToSignInCourse({sr_id:id,sign_type:'课程',course_msg:course});
    }).catch(() => {
      //console.log('cancel');
    });
  },
  /**
  * @function 通过课程id找到对应课程
  * @param {*id,*c_msg} 课程id 课程信息
  */
  _getCouresFromId(sr_id:number,c_msg:Array<Course>):Course{
    for(let i =0;i<c_msg.length;i++){
      if(c_msg[i].sr_id==sr_id) return c_msg[i];
    }
    return {} as Course;
  },
  /**
  * @function 根据学期开始日期判断今天属于哪一周
  * @param {*startDate} 学期开始日期
  */
  _getWeekNumber(startDate: string): number {
    const today = new Date(); // 获取当前日期
    const inputDate = new Date(startDate);
    if (today > inputDate) {
      const timeDiff = Math.abs(today.getTime() - inputDate.getTime());
      const daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24)); // 计算日期差
      if (daysDiff >= 18) {
        return 18;
      } else {
        const groupNumber = Math.ceil(daysDiff / 7);
        return groupNumber;
      }
    } else {
      this.setData({
        sign_valid:false,
      })
      return 1;
    }
  },

  switchWeek(e: any) {
    let index = parseInt(e.detail.value);
    this.setData({
      nowWeeks: index + 1,
    });
    this._getSchFromWeek(index + 1);
  },
  /**
   * @function 计算显示这一周的什么课程
   * @param {*nowWeeks} 显示该学期第几周的课表
   */
  _getSchFromWeek(nowWeeks: number) {
    this._getPreciseDates(this.data.startDate, nowWeeks);
    //console.log('nowWeeks=',nowWeeks);
    this._showCurriculums(nowWeeks, this.data.curriculum_msg);
  },
  /**
   * @function 返回输入日期的周一的日期
   * @return 日期字符串
   */
  _getMondayDate(inputDate: string) {
    const date = new Date(inputDate);
    const dayOfWeek = date.getDay(); // 获取输入日期是星期几
    const mondayTime = date.getTime() - (dayOfWeek - 1) * 24 * 60 * 60 * 1000; // 找到那一周的周一的时间戳
    const mondayDate = new Date(mondayTime).toLocaleDateString(); // 转换为日期字符串

    return mondayDate;
  },

  /**
   * @function 根据输入的日期和过去的周数，获取并返回对应周的日期范围（从周一到周日）,即显示在课程表第一层的日期
   * @param {*startDate} 开始时间
   * @param {*nowWeeks} 第几周
   */
  _getPreciseDates(startDate: string, nowWeeks: number): string[] {
    const oneDay = 24 * 60 * 60 * 1000; // 毫秒
    const mondayDate = new Date(this._getMondayDate(startDate)); // 找到开始日期的周一的日期
    mondayDate.setDate(mondayDate.getDate() + (7 * (nowWeeks - 1))); // 根据过去的周数计算所需周的周一日期
    const weekDates: string[] = [];
    for (let i = 0; i < 7; i++) {
      const currentDate = new Date(mondayDate.getTime() + i * oneDay);
      const month = currentDate.getMonth() + 1;
      const day = currentDate.getDate();
      weekDates.push(`${month}/${day}`);
    }
    this.setData({
      precise: weekDates,
    })
    return weekDates;
  },
  /**
   * @function 根据课程信息，更改前端课程表
   * @param {*curriculum_msg} 课程信息
   */
  _showCurriculums(nowWeeks: number, curriculum_msg: Array<Course>) {
    //1检查这一周有没有这节课2更改curriculum里对应前端信息
    let c_temp = curriculum_msg;
    let c_exist: Array<Course> = [];
    //执行1
    c_temp.forEach(element => {
      //如果有上课时间才显示
      if(element.week&&element.day&&element.time){
        if (element.week== nowWeeks) {
          c_exist.push(element);
        }
      }
    });
    //console.log('c_exist=',c_exist);
    //执行2
    let colors = this.data.colors;
    let new_curriculu = {
      "一": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "二": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "三": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "四": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "五": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "六": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
      "七": {
        1: { background_color: "", border_radius: "", text: "",id:0 },
        2: { background_color: "", border_radius: "", text: "",id:0 },
        3: { background_color: "", border_radius: "", text: "",id:0 },
        4: { background_color: "", border_radius: "", text: "",id:0 },
        5: { background_color: "", border_radius: "", text: "",id:0 },
        6: { background_color: "", border_radius: "", text: "",id:0 },
        7: { background_color: "", border_radius: "", text: "",id:0 },
        8: { background_color: "", border_radius: "", text: "",id:0 },
        9: { background_color: "", border_radius: "", text: "",id:0 },
        10: { background_color: "", border_radius: "", text: "",id:0 },
        11: { background_color: "", border_radius: "", text: "",id:0 },
      },
    } as Curriculum;
    //console.log('curriculum_default=',this.data.curriculum_default);
    c_exist.forEach((item, index) => {
      let start = parseInt(item.time.split('-')[0]);
      let end = parseInt(item.time.split('-')[1]);
      //给课程上颜色
      for (let i = start; i <= end; i++) {
        new_curriculu[item.day][i].background_color = colors[index];
        new_curriculu[item.day][i].id=item.sr_id;
      }
      //上弧形边角
      new_curriculu[item.day][start].border_radius = "15rpx 15rpx 0 0";
      new_curriculu[item.day][end].border_radius = "0 0 15rpx 15rpx";
      if(start==end)
      new_curriculu[item.day][start].border_radius= "15rpx";
      //上文字
      new_curriculu[item.day][start].text = item.name + "@" + item.location;
      //console.log('text=',new_curriculu[item.day][start].text);
      //console.log('colors=',colors[index]);
    });
    //给前端赋值
    this.setData({
      curriculum: new_curriculu,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(){

  },
  /**
   * @function 将阿拉伯数字转换为简体中文数字
   */
  _convertToChinese(number:string) {
    var chineseNumber = '';
    switch (number) {
      case '1':
        chineseNumber = '一';
        break;
      case '2':
        chineseNumber = '二';
        break;
      case '3':
        chineseNumber = '三';
        break;
      case '4':
        chineseNumber = '四';
        break;
      case '5':
        chineseNumber = '五';
        break;
      case '6':
        chineseNumber = '六';
        break;
      case '7':
        chineseNumber = '七';
        break;
    }
    
    return chineseNumber;
  },
  onClickLeft(){
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    msg.Loading('获取课表信息中');
    let nowWeeks=app.globalData.nowWeeks;
    let startDate=app.globalData.startDate;
    let user = new User;
    curriculumInit({code:user.code})
    .then((res:any)=>{
      msg.hideLoading()
      if(res.data.code!=200){
        msg.show('加载课程表失败','请检查网络设置');return;
      }
      this.setData({
        curriculum_msg:res.data.data,
        startDate:startDate,
        nowWeeks:nowWeeks,
      });
      this._getSchFromWeek(nowWeeks);

    })
    //.catch((err:any)=>{msg.show('加载失败','请检查网络设置');});
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
